<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\City;

class CityController extends H5Controller
{
    /**
     * city tree
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionTree()
    {
        $path = __DIR__ . '/cache/tree.json';

        if (!file_exists($path)) {
            $tree = City::getTree();
            $tree = json_encode($tree);
            file_put_contents($path, $tree);
        }

        $data = file_get_contents($path);
        $filesize = filesize($path);

        return $this->success([
            'tree' => json_decode($data, true),
            'Bytes' => $filesize,
            'KB' => round($filesize / 1024, 2),
        ]);
    }
}